var crypto  = require("crypto")
,   core    = require("core")
,   db      = core.db
,   schema  = db.schema;

var login = {
    path    : "/admin/login",
    method  : "post",
    auth    : function(req, next){
        var body = req.body
        ,   name = body.name
        ,   pwd  = body.pwd + ""
        ,   sha1 = crypto.createHash('sha1');
        sha1.update(pwd);
        pwd = sha1.digest('hex');

        var User = {
            name : name,
            pwd  : pwd
        };

        schema.User.findOne(User, function(err, docs){
            if(err || !docs){
                var token = core.createToken(3000, {error: "用户名或密码错误"});
                return next(new Error(), "/admin/login?token="+ token);
            };
            next();
        });
    }
};

module.exports = login;
